package com.zjdiepu.www.manage.api.file;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.biz.ibiz.IFileHelpService;
import com.zjdiepu.www.biz.ibiz.IOSSFileSerivce;
import com.zjdiepu.www.commpent.OSSCommpent;
import com.zjdiepu.www.config.OSSProperty;

/**
 * 项目名称：dp-rest <br/> 
 * 类名称：OSSController <br/>  
 * 类描述：oss操作<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年3月30日 上午9:36:41  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年3月30日 上午9:36:41   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "OSSController", produces = "application/json;charset=UTF-8")
public class OSSController extends BaseController {
	
	@Autowired
	private OSSProperty ossProperty;
	@Autowired
	private IFileHelpService fileHelpService;
	@Autowired
	private IOSSFileSerivce ossFileSerivce;
	@Resource
	private OSSCommpent ossCommpent;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: oss  <br/>
	 * @Description: OSS文件上传  <br/>
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = { "upload" }, method = { RequestMethod.POST })
	public ApiResponse upload(@RequestParam("uploadfile") MultipartFile file, @RequestParam(defaultValue="0")Integer imageType, @RequestParam(defaultValue="M_qt")String modeId) {
		ResultFlagType checkPath = fileHelpService.checkPath(file, imageType);
		if(!ResultFlagType.SUCCESS.equals(checkPath)) {
			return ApiResponse.fail(checkPath);
		}
		AbstractSysUser currentUser = getCurrentUser();
		String savePath = fileHelpService.getPathFromType(imageType);
		return ossFileSerivce.saveToCloud(file, currentUser.getId(), currentUser.getName(), savePath, modeId);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 阿里云OSS：获取获取临时授权url<br/>
	 * @param juheProvinceExt
	 * @return
	 * @version V1.0  <br/>
	 * @throws HttpClientException 
	 * @throws IOException 
	 */
	@RequestMapping(value = "url", method = { RequestMethod.POST })
	public ApiResponse url(String fileKey) throws HttpClientException, IOException {
		if(StringUtils.isBlank(fileKey)) {
			return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
		}
		return ApiResponse.success(ossCommpent.getUrlAndAuthorized(fileKey));
	}
}
